home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Dialog Boxes / SimpleDialog / SimpleDialog.cs next >
Encoding:
Text File  |  2001-01-15  |  2.2 KB  |  80 lines

  1. //-------------------------------------------
  2. // SimpleDialog.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class SimpleDialog: Form
  9. {
  10.      string strDisplay = "";
  11.  
  12.      public static void Main()
  13.      {
  14.           Application.Run(new SimpleDialog());
  15.      }
  16.      public SimpleDialog()
  17.      {
  18.           Text = "Simple Dialog";
  19.  
  20.           Menu = new MainMenu();
  21.           Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
  22.      }
  23.      void MenuOnClick(object obj, EventArgs ea)
  24.      {
  25.           SimpleDialogBox dlg = new SimpleDialogBox();
  26.  
  27.           dlg.ShowDialog();
  28.  
  29.           strDisplay = "Dialog box terminated with " + 
  30.                             dlg.DialogResult + "!";
  31.           Invalidate();
  32.      }
  33.      protected override void OnPaint(PaintEventArgs pea)
  34.      {
  35.           Graphics grfx = pea.Graphics;
  36.           grfx.DrawString(strDisplay, Font, new SolidBrush(ForeColor), 0, 0);
  37.      }
  38. }
  39. class SimpleDialogBox: Form
  40. {
  41.      public SimpleDialogBox()
  42.      {
  43.           Text = "Simple Dialog Box";
  44.  
  45.                // Standard stuff for dialog boxes
  46.  
  47.           FormBorderStyle = FormBorderStyle.FixedDialog;
  48.           ControlBox      = false;
  49.           MaximizeBox     = false;
  50.           MinimizeBox     = false;
  51.           ShowInTaskbar   = false;
  52.  
  53.                // Create OK button.
  54.  
  55.           Button btn = new Button();
  56.           btn.Parent   = this;
  57.           btn.Text     = "OK";
  58.           btn.Location = new Point(50, 50);
  59.           btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
  60.           btn.Click   += new EventHandler(ButtonOkOnClick);
  61.  
  62.                // Create Cancel button.
  63.  
  64.           btn = new Button();
  65.           btn.Parent   = this;
  66.           btn.Text     = "Cancel";
  67.           btn.Location = new Point(50, 100);
  68.           btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
  69.           btn.Click   += new EventHandler(ButtonCancelOnClick);
  70.      }
  71.      void ButtonOkOnClick(object obj, EventArgs ea)
  72.      {
  73.           DialogResult = DialogResult.OK;
  74.      }
  75.      void ButtonCancelOnClick(object obj, EventArgs ea)
  76.      {
  77.           DialogResult = DialogResult.Cancel;
  78.      }
  79. }
  80.